// 权限管理模块
import { constantRoutes } from '@/router'
import { asyncRoutes } from '@/router'
const state = {
  // 默认存储的是静态路由映射
  routes: constantRoutes
}
const mutations = {
  updateRoutes (state, routes) {
    // routes表示动态路由(把传递过来的动态路由和静态路由进行合并)
    state.routes = [...constantRoutes, ...routes]
  }
}
const actions = {
  filterAuth (context, menus) {
    // 过滤用户的路由菜单的权限
    const myRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].name))

    // 把过滤出来的动态路由添加到vuex的状态里面
    context.commit('updateRoutes', myRoutes)

    // 返回过滤的动态路由
    return myRoutes
  }
}
export default {
  // 命名空间，把模块变成局部的
  namespaced: true,
  state,
  mutations,
  actions
}
